package com.yixin.hospital.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.Job;
import com.yixin.hospital.entity.domain.JobTitle;
import com.yixin.hospital.entity.dto.req.jobtitle.JobTitleAddReqDTO;
import com.yixin.hospital.entity.dto.req.jobtitle.JobTitleEditReqDTO;
import com.yixin.hospital.entity.vo.JobTitleChildrenVO;
import com.yixin.hospital.entity.vo.JobTitleListTreeVO;
import com.yixin.hospital.entity.vo.JobTitleTreeVO;
import com.yixin.hospital.entity.vo.JobTitleVO;
import com.yixin.hospital.service.JobService;
import com.yixin.hospital.service.JobTitleService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/job-title")
@Api(value = "职称管理", tags = "职称管理")
public class JobTitleController {

    @Resource
    JobTitleService jobTitleService;
    @Resource
    JobService jobService;

    @GetMapping("/list")
    @ApiOperation(value = "职称列表")
    public ApiResult list() {
        List<JobTitle> list = jobTitleService.list();
        List<JobTitleVO> jobTitleVOS = BeanUtil.copyToList(list, JobTitleVO.class);
        for (JobTitleVO jobTitleVO : jobTitleVOS) {
            Job job = jobService.getById(jobTitleVO.getJobId());
            jobTitleVO.setJobName(job.getName());
        }
        return ApiResult.success(jobTitleVOS);
    }

    @GetMapping("/list-tree")
    @ApiOperation(value = "职称列表(树状)")
    public ApiResult listTree() {
        JobTitleListTreeVO jobTitleListTreeVO = new JobTitleListTreeVO();

        List<JobTitleTreeVO> jobTitleTreeVOList = new ArrayList<>();

        List<Job> jobList = jobService.list();
        for (Job job : jobList) {
            // 职业
            JobTitleTreeVO jobTitleTreeVO = new JobTitleTreeVO();
            jobTitleTreeVO.setId(job.getId());
            jobTitleTreeVO.setLabel(job.getName());
            jobTitleTreeVO.setLevel(2);
            // 职称
            LambdaQueryWrapper<JobTitle> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(JobTitle::getJobId, job.getId());
            List<JobTitle> jobTitleList = jobTitleService.list(queryWrapper);
            List<JobTitleChildrenVO> children = new ArrayList<>();
            for (JobTitle jobTitle : jobTitleList) {
                JobTitleChildrenVO childrenVO = new JobTitleChildrenVO();
                childrenVO.setId(jobTitle.getId());
                childrenVO.setLabel(jobTitle.getName());
                childrenVO.setLevel(3);
                children.add(childrenVO);
            }
            jobTitleTreeVO.setChildren(children);

            jobTitleTreeVOList.add(jobTitleTreeVO);
        }

        jobTitleListTreeVO.setChildren(jobTitleTreeVOList);

        return ApiResult.success(Arrays.asList(jobTitleListTreeVO));
    }

    @GetMapping("/job-job-title")
    @ApiOperation(value = "根据职业查询职称")
    @ApiImplicitParams({
            @ApiImplicitParam(name="jobId",value="职业id", paramType = "query")
    })
    public ApiResult deptWorkAddress(@RequestParam Long jobId) {
        LambdaQueryWrapper<JobTitle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JobTitle::getJobId, jobId);
        List<JobTitle> list = jobTitleService.list(queryWrapper);
        return ApiResult.success(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加职称")
    public ApiResult add(@Valid @RequestBody JobTitleAddReqDTO jobTitleAddReqDTO, HttpServletRequest request) {
        JobTitle jobTitle = new JobTitle();
        jobTitle.setName(jobTitleAddReqDTO.getName());
        jobTitle.setJobId(jobTitleAddReqDTO.getJobId());
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        jobTitle.setCreatedBy(userName);
        jobTitle.setUpdatedBy(userName);
        jobTitleService.save(jobTitle);
        return ApiResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑职称")
    public ApiResult edit(@Valid @RequestBody JobTitleEditReqDTO jobTitleEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        JobTitle jobTitle = new JobTitle();
        jobTitle.setId(jobTitleEditReqDTO.getId());
        jobTitle.setName(jobTitleEditReqDTO.getName());
        jobTitle.setUpdatedBy(userName);
        jobTitleService.updateById(jobTitle);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除职称")
    public ApiResult remove(@PathVariable int id) {
        jobTitleService.removeById(id);
        return ApiResult.success();
    }
}
